package com.saianfu.flowable.demo.controller

import com.saianfu.flowable.demo.common.impl.HistTaskQueryHandler
import com.saianfu.flowable.demo.common.impl.TaskQueryHandler
import com.saianfu.flowable.demo.controller.result.ResponseData
import io.swagger.annotations.Api
import io.swagger.annotations.ApiImplicitParam
import io.swagger.annotations.ApiImplicitParams
import io.swagger.annotations.ApiOperation
import org.flowable.engine.HistoryService
import org.flowable.engine.TaskService
import org.flowable.task.api.history.HistoricTaskInstance
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.RestController

/**
 *
 *
 * 流程任务相关
 *
 */
@RestController
@RequestMapping("api/flow/query")
@Api(value = "Query", tags = ["查询相关"])
class QueryController {
    @Autowired
    lateinit var taskService: TaskService

    @Autowired
    lateinit var historyService: HistoryService

    @Autowired
    lateinit var taskQueryHandler: TaskQueryHandler

    @Autowired
    lateinit var histTaskQueryHandler: HistTaskQueryHandler

    @RequestMapping(value = ["/task/list/instanceId"], method = [RequestMethod.GET])
    @ResponseBody
    @ApiOperation(value = "查询任务列表（所有）", produces = "application/json")
    @ApiImplicitParams(ApiImplicitParam(name = "instanceId", value = "流程实例ID", required = true, dataType = "String"))
    fun listByInstanceId(instanceId: String?): Any {
        val list = histTaskQueryHandler.listByInstanceId(instanceId!!)
        return ResponseData.success(list)
    }

    @RequestMapping(value = ["/task/pageList/instanceId"], method = [RequestMethod.GET])
    @ResponseBody
    @ApiOperation(value = "分页查询任务列表（所有）", produces = "application/json")
    @ApiImplicitParams(ApiImplicitParam(name = "instanceId", value = "流程实例ID", required = true, dataType = "String"), ApiImplicitParam(name = "page", value = "页码", required = false, defaultValue = "1", dataType = "int"), ApiImplicitParam(name = "step", value = "数量", required = false, defaultValue = "20", dataType = "int"))
    fun pageListByInstanceId(instanceId: String, page: Int, limit: Int): Any {
        val list = histTaskQueryHandler.pageListByInstanceId(instanceId, page, limit)
        return ResponseData.success(list)
    }

    @RequestMapping(value = ["/task/list/unclaim"], method = [RequestMethod.GET])
    @ResponseBody
    @ApiOperation(value = "查询未签收任务列表", produces = "application/json")
    @ApiImplicitParams(ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String"), ApiImplicitParam(name = "page", value = "页码", required = false, defaultValue = "1", dataType = "int"), ApiImplicitParam(name = "step", value = "数量", required = false, defaultValue = "20", dataType = "int"))
    fun unclaim(userId: String, page: Int, limit: Int): Any {
        val taskList = taskQueryHandler.taskCandidateOrAssigned(userId, page, limit)
        //List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery().taskCandidateUser(userId).finished().list();
        return ResponseData.success(taskList)
    }

    @RequestMapping(value = ["/task/list/claimed"], method = [RequestMethod.GET])
    @ResponseBody
    @ApiOperation(value = "查询已签收任务列表", produces = "application/json")
    @ApiImplicitParams(ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String"))
    fun claimed(userId: String): Any {
        val list = historyService.createHistoricTaskInstanceQuery().taskAssignee(userId).list()
        return ResponseData.success(list)
    }

    @RequestMapping(value = ["/task/active"], method = [RequestMethod.GET])
    @ResponseBody
    @ApiOperation(value = "根据实例ID，查询当前活动任务", produces = "application/json")
    @ApiImplicitParams(ApiImplicitParam(name = "instanceId", value = "实例ID", required = true, dataType = "String"))
    @Throws(Exception::class)
    fun query(instanceId: String): ResponseData<HistoricTaskInstance?> {
        //单实例任务--单个活动任务
        //方式一：需要处理懒加载问题
        //Task task = taskQueryHandler.processInstanceId(instanceId);
        //TaskVO taskVO = new TaskVO();
        //BeanUtils.copyProperties(task, taskVO);

        //方式二：正常使用，通过historyService查询当前活动任务，无需进行转换
        val historicTaskInstance = historyService
                .createHistoricTaskInstanceQuery()
                .processInstanceId(instanceId)
                .unfinished().singleResult()

        //多实例并行任务--多个活动任务
        //List<Task> list = taskService.createTaskQuery().processInstanceId(instanceId).active().list();
        return ResponseData.success(historicTaskInstance)
    }

    companion object {
        protected var logger = LoggerFactory.getLogger(QueryController::class.java)
    }
}